package it.dgtest.server.ibatis;

import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class IBatisUtil {

    private static SqlSessionFactory factory;

    private IBatisUtil() throws Exception {
    }

    public static SqlSession openSession() {
    	try {
	        if (factory == null) {
	            String resource = "ibatis.cfg.xml";
	            Reader reader = Resources.getResourceAsReader(resource);
	            factory = new SqlSessionFactoryBuilder().build(reader,"development");
	        }
	        return factory.openSession();
    	} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
    }
    
    public void closeSession(SqlSession session) {
        if (session!=null) session.close();
    }

}

